<?php
	$goto = isset($_SESSION['return_path']) ? $_SESSION['return_path'] : $path.$table;
	
	$data = $cn->Data('select * from subscription_types where id='.$post_id, false);

	if (sizeof($data) > 0) {
		$label = $data[0]['label'];
		$duration = $data[0]['duration'];
		$price = $data[0]['price'];

		// get next id for invoice number
		$inv = $cn->Data('select id from invoices order by id desc limit 1', false);
		if (sizeof($inv) == 0) $nextid = 1;
		else $nextid = intval($inv[0]['id']) + 1;
		$inv = null;

		$now = date("Y-m-d H:i:s");
		//$enddate = date('Y-m-d H:i:s', strtotime($now. ' + '.$duration.' days'));
		$enddate = date('Y-m-d H:i:s', strtotime($now. ' + 1 month'));
		$invno = 'SUB'.date('Ym').str_pad($nextid, 4, '0', STR_PAD_LEFT);

		$paid = 0;
		
		// Pay As You Go
		if ($price == 0) {
			$paid = 1;
			// create invoice, get invoice number add as reference.
			$res = $cn->Add('invoices', array('date'=>$now, 'userid'=>$user_id, 'label'=>$invno, 'amount'=>$price, 'content'=>'New subscription', 'paid'=>1, 'completed'=>1), false);
						
			// create a log of transaction
			$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>'New subscription complete. Invoice ['.$invno.']', 'type'=>'subscribe'), false);

		} else {
			// create invoice, get invoice number add as reference.
			$res = $cn->Add('invoices', array('date'=>$now, 'userid'=>$user_id, 'label'=>$invno, 'amount'=>$price, 'content'=>'New subscription [pending]', 'paid'=>0, 'completed'=>0), false);

			// create a log of transaction
			$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>'New subscription pending. Invoice ['.$invno.']', 'type'=>'subscribe'), false);

			$user_email = $userdata['email'];
			$return_url = 'http://'.$server.$path.$table.'/confirm';
			$price = str_replace('.', '', $price);
			$secretKey = md5("10011013800|$invno|$price|ZAR|$return_url|$now|$user_email|secret");//PAYGATE_ID|REFERENCE|AMOUNT|CURRENCY|RETURN_URL|TRANSACTION_DATE|EMAIL|KEY

			$str = '<h2>Confirm Subscription Purchase</h2>
				<h3>Amount due: R'.($price / 100).'.00</h3>
				<ul class="details grey">
					<li>Details</li><li>'.$label.'</li>
					<li>Start Date</li><li>'.$now.'</li>
					<li>End Date</li><li>'.$enddate.'</li>
				</ul>
				<p>You are about to be redirect to a secure <a href="http://www.paygate.co.za" target="_blank">PayGate</a> page to complete this transaction.<br/>On completition of the transaction, using your prefered payment method, you will be returned to your dashboard.</p>
				<p>Please note: The Sett does not store any payment details. All transactions are handled using <a href="http://www.paygate.co.za" target="_blank">PayGate</a>.</p>
			<form action="https://www.paygate.co.za/paywebv2/process.trans" method="POST">
			<input type="hidden" name="PAYGATE_ID" value="10011013800">
			<input type="hidden" name="REFERENCE" value="'.$invno.'">
			<input type="hidden" name="AMOUNT" value="'.$price.'">
			<input type="hidden" name="CURRENCY" value="ZAR">
			<input type="hidden" name="RETURN_URL" value="'.$return_url.'">
			<input type="hidden" name="TRANSACTION_DATE" value="'.$now.'">
			<input type="hidden" name="EMAIL" value="'.$user_email.'">
			<input type="hidden" name="CHECKSUM" value="'.$secretKey.'">
			<input type="submit" value="Proceed" class="button" id="PayGateSubmit">
			</form>';
			echo $str;
		}

		// check if subscription for user exists already and update
		
		$res = $cn->Data('select id from subscriptions where userid='.$user_id, false);
		if (sizeof($res) == 0) {
			$res = $cn->Add('subscriptions', array('userid'=>$user_id, 'subscriptiontypeid'=>$post_id, 'startdate'=>$now, 'enddate'=>$enddate, 'paid'=>$paid), false);
		} else {
			$subid = $res[0]['id'];
			$res = $cn->Update('subscriptions', $subid, array('subscriptiontypeid'=>$post_id, 'startdate'=>$now, 'enddate'=>$enddate, 'paid'=>$paid), false);
		}
		$res = null;

		if ($price == 0) header('location: '.$goto);

	} else {
		header('location: '.$goto);
	}
?>